package cn.itcast.user.service;

import cn.afterturn.easypoi.word.WordExportUtil;
import cn.hutool.core.collection.CollUtil;
import cn.itcast.user.mapper.UserMapper;
import cn.itcast.user.pojo.QuestionVO;
import cn.itcast.user.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public User queryById(Long id) {
        return userMapper.findById(id);
    }

    public void importExcel(List<QuestionVO> list) {
        log.info("导入数据：{}条", list.size());
        List<List<QuestionVO>> split = CollUtil.split(list, 100);
        List<Map<String, Object>> excelList = new ArrayList<>();
        for (int i = 0; i < split.size(); i++) {
            List<QuestionVO> sheet = split.get(i);
            QuestionVO question1 = new QuestionVO();
            question1.setNumber("-----分割线内禁止答题-----");
            question1.setQuestion("-----分割线内禁止答题-----");
            question1.setAnswer("-----分割线内禁止答题-----");
//            sheet.add(question1);
            for (QuestionVO questionVO : sheet) {
                String question = questionVO.getQuestion();
                // 去除中文空格，英文空格，中文括号替换为英文括号　
                question = question.replaceAll("　", "")
                        .replaceAll(" ", "")
                        .replaceAll("（）", "()");
                String answer = questionVO.getAnswer();
                int length = answer.length() * 5;
                question = question.replaceAll("\\(\\)", "\\(" + getCharacter(length, " ") + "\\)");
                // 判断题加括号
                if (Integer.parseInt(questionVO.getNumber()) > 80) {
                    question = question + "    (    )";
                }
                questionVO.setQuestion(question);
                questionVO.setNumber(questionVO.getNumber() + ".");
            }
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            map.put("list", sheet);
            map.put("title", "车站值班员（中级）" + (i + 1));
            excelList.add(map);
        }

        try {
            XWPFDocument doc = WordExportUtil.exportWord07("C:\\Users\\wo\\Desktop\\1.docx", excelList);
            FileOutputStream fos = new FileOutputStream("C:\\Users\\wo\\Desktop\\simple.docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取指定数量的字符
     *
     * @param length    字符数量
     * @param character 要获取的字符
     * @return 空格
     */
    private String getCharacter(int length, String character) {
        StringBuilder space = new StringBuilder();
        for (int i = 0; i < length; i++) {
            space.append(character);
        }
        return space.toString();
    }
}